/**
 * 正则
 **/
var mbTest = /^1(3|4|5|6|7|8|9)\d{9}$/; // 手机号
var bank_reg = /^(\d{16}|\d{17}|\d{18}|\d{19})$/;  //银行卡号
var myPwd = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/;  // 8-16位数密码
var isIdCard1 = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/;  //身份证验证
var isIdCard2 = /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}[A-Za-z0-9]{1}$/; //身份证验证正则
var HK_idcard = /^([A-Za-z])|([A-Za-z]{2})\d{6}(([0−9aA])|([0-9aA]))$/;    //匹配香港身份证号
var email_reg = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/; // email
var stringReg = /^[A-Za-z0-9]+$/;
var minSexNumber = /^\d{6,}$/;


// 验证手机号码
function checkPhone() {
    var str = $('#mobile').val();
    if (!mbTest.test(str)) {
        layer.alert("手机号码格式不正确", {icon: 2}, function (index) {
            layer.close(index);
            $("#mobile").focus();
        });
        return false;
    } else {
        return true;
    }
}

// 验证图形验证码
function checkImgCode() {
    var eCode = $("#imgCode").val();
    if (eCode.length !== 4) {
        layer.alert("请输入4位图形验证码", {icon: 2}, function (index) {
            layer.close(index);
            $("#imgCode").focus();
        });
        return false;
    } else {
        return true;
    }
}

//检验手机验证码
function checkPhoneCode() {
    var phoneCode = $("#phoneCode").val();
    if (phoneCode.length != 6) {
        layer.alert("请输入6位短信验证码", {icon: 2}, function (index) {
            layer.close(index);
            $("#phoneCode").focus();
        });
        return false;
    } else {
        return true;
    }
}


// 检验密码
function checkPwd() {
    var password = $("#pwd").val();
    if (!myPwd.test(password)) {
        layer.alert("请输入8-16个字母、数字密码", {icon: 2}, function (index) {
            layer.close(index);
            $("#pwd").focus();
        });
        return false;
    } else {
        return true;
    }
}


// 检验中文姓名
function checkCnName() {
    var name = $("#actualName").val();
    var m = name.match(/^[\u4e00-\u9fa5]{2,20}$/i);  //中文名字2-20位
    // 验证真实姓名
    if (name.length === 0 || !m) {
        layer.alert("请输入真实姓名", {icon: 2}, function (index) {
            layer.close(index);
            $("#actualName").focus();
        });
        return false;
    } else {
        return true;
    }
}

// 检验身份证
function checkIdCode() {
    var idCard1 = $("#idCard1").val();
    var idCard2 = $("#idCard2").val();
    if (!isIdCard1.test(idCard1) && !isIdCard2.test(idCard1) && !HK_idcard.test(idCard1)) {
        layer.alert("请输入正确的身份证号", {icon: 2}, function (index) {
            layer.close(index);
            $("#idCard1").focus();
        });
        return false;
    }
    if (!isIdCard1.test(idCard2) && !isIdCard2.test(idCard2) && !HK_idcard.test(idCard2)) {
        layer.alert("请输入正确的身份证号", {icon: 2}, function (index) {
            layer.close(index);
            $("#idCard2").focus();
        });
        return false;
    }
    if (idCard1 !== idCard2) {
        layer.alert("两次输入的身份证号不一致", {icon: 2}, function (index) {
            layer.close(index);
            $("#idCard2").focus();
        });
        return false;
    }
    return true;
}

// 单次检验身份证
function checkIdCodeOne() {
    var idCard = $("#idCard").val();
    if (!isIdCard1.test(idCard) && !isIdCard2.test(idCard)) {
        layer.alert("请输入正确的身份证号", {icon: 2}, function (index) {
            layer.close(index);
            $("#idCard").focus();
        });
        return false;
    }
    return true;
}

// email
function checkEmail() {
    var id = "#email";
    var email = $(id).val();
    if (!email_reg.test(email)) {
        layer.alert("请输入正确的email格式!", {title: "提示"}, function (index) {
            $(id).focus();
            layer.close(index);
        });
        return false;
    }
    return true;
}



//  验证支付密码
function check_pay_pass(pass) {
    var payPwd = /^[0-9]{6}$/;  // 支付密码
    if(!payPwd.test(pass)){
        layer.tips('请输入6位数字支付密码！', '#password-input', {tips: 1,time: 4000});
        return false;
    }
    return true;
}

// 短信验证码倒计时
function settTime() {
    var intTime = 59;  //秒数
    var beginTime;
    beginTime = setInterval(function () {
        var o = $("#sendMsgBtn");
        if (intTime === 0) {
            clearInterval(beginTime);
            o.attr({'disabled': false});
            o.removeClass('not');
            o.val('获取验证码');
            intTime = 59;
        } else {
            o.addClass('not');
            o.attr({'disabled': true});
            o.val("重发短信验证码(" + intTime + ")");
            intTime--;
        }
    }, 1000);
}

// 设置密码
function setPassword() {
    var pwd1 = $("#pwd1").val();
    var pwd2 = $("#pwd2").val();
    if (!myPwd.test(pwd1)) {
        layer.alert("请设置8-16个字母和数字新密码！", {icon: 2}, function (index) {
            layer.close(index);
            $("#pwd1").focus();
        });
        return false;
    }
    if (!myPwd.test(pwd2)) {
        layer.alert("请重复8-16个字母和数字新密码！", {icon: 2}, function (index) {
            layer.close(index);
            $("#pwd2").focus();
        });
        return false;
    }
    if (pwd1 !== pwd2) {
        layer.alert("两次输入的密码不一致！", {icon: 2}, function (index) {
            layer.close(index);
            $("#pwd2").focus();
        });
        return false;
    }
    return true;
}

// 银行卡号码
function checkBank() {
    var bank = $("#bank").val();
    if (!bank_reg.test(bank)) {
        layer.alert("请输入正确的绑定银行卡号", {icon: 2}, function (index) {
            layer.close(index);
            $("#bank").focus();
        });
        return false;
    }
    return true;
}


function checkBankArea() {
    var province = $("#s_province").val();
    var city = $("#s_city").val();
    var county = $("#s_county").val();
    if (province == '') {
        layer.layer("请选择开户行所在省份!");
        return false;
    }
    if (city == '') {
        layer.layer("请选择开户行所在城市!");
        return false;
    }
    if (county == '') {
        layer.layer("请选择开户行所在地区!");
        return false;
    }
    return true;
}

// 支行地址
function checkaddress() {
    var adress = $("#card_address").val();
    var adressreg = new RegExp("^[\u4e00-\u9fa5]{2,20}$");
    var m = adressreg.test(adress);  //支行名称为2-20位中文字符
    var m2 = adress.slice(adress.length - 2, adress.length);  // 支行截取 最后两个字符
    if (!m) {
        layer.alert("请输入中文开户支行名称", {icon: 2}, function (index) {
            layer.close(index);
            $("#card_address").focus();
        });
        return false;
    }
    if (!(m2 == "支行" || m2 == "分行")) {
        layer.alert("支行名称必须以“支行”或“分行”结尾", {icon: 2}, function (index) {
            layer.close(index);
            $("#card_address").focus();
        });
        return false;
    }
    return true;
}

// 修改密码
function editPassWord() {
    var pwd0 = $("#pwd0").val();
    var pwd1 = $("#pwd1").val();
    var pwd2 = $("#pwd2").val();

    if (pwd0 == "" || pwd0.length < 6) {
        layer.alert("请输入原密码！", {icon: 2}, function (index) {
            layer.close(index);
            $("#pwd0").focus();
        });
        return false;
    }
    if (!myPwd.test(pwd1)) {
        layer.alert("请设置8-16个字母和数字新密码！", {icon: 2}, function (index) {
            layer.close(index);
            $("#pwd1").focus();
        });
        return false;
    }
    if (pwd0 == pwd1) {
        layer.alert("新密码不能与原密码一致!", {icon: 2}, function (index) {
            layer.close(index);
            $("#pwd1").focus();
        });
        return false;
    }
    if (!myPwd.test(pwd2)) {
        layer.alert("请再次输入8-16个字母和数字新密码！", {icon: 2}, function (index) {
            layer.close(index);
            $("#pwd2").focus();
        });
        return false;
    }
    if (pwd1 !== pwd2) {
        layer.alert("两次输入的密码不一致！", {icon: 2}, function (index) {
            layer.close(index);
            $("#pwd2").focus();
        });
        return false;
    }
    return true;
}

// 发送短信ajax
function sendMobileMsgCode(sendCodeUrl, phone, vCode, type) {
    $.ajax({
        url: sendCodeUrl,
        type: "post",
        dataType: "json",
        data: {phone: phone, verify: vCode, type: type},
        success: function (data) {
            if (data.code == 1) {
                settTime();
                layer.alert(data.msg, {icon: 1});
            } else {
                layer.alert(data.msg, {icon: 2});
                $('.reloadVerify').click();
                $('#imgCode').val('');
                return false;
            }
        }
    });
}

//检查充值提现金额
function checkMoney(value) {


    if (value >= 1) {
        var index = value.indexOf(".");
        if (index == 0) {
            $('.submitBtn').prop('disabled', '').removeClass('disabled');
            return "";
        }
        if (index != -1) {
            var arr = value.split(".");
            var h = "", b = "";
            for (var i = 0; i < arr.length; i++) {
                if (i == 0) {
                    h = arr[i].replace(/\D/g, '') + ".";
                } else {
                    b += "" + arr[i].replace(/\D/g, '');
                    if (b.length >= 2) {
                        break;
                    }
                }
            }
            if (h.charAt(0) == "0") {
                h = "0.";
            }
            value = h + b.substr(0, 2);
            $('.submitBtn').prop('disabled', '').removeClass('disabled');
        } else {
            value = value.replace(/\D/g, '');
            if (value.charAt(0) == "0") {
                value = parseInt(value, 10);
            }
            $('.submitBtn').prop('disabled', '').removeClass('disabled');
        }
        return value == "0.00" ? "0" : value;
    }
    else {
        $('.submitBtn').prop('disabled', 'disabled').addClass('disabled');
        return value = "";
    }
}



// 验证金额
function checkRechargeMoney(id, txt) {
    var input = $(id);
    var value = parseFloat(input.val());
    if (isNaN(value)) {
        layer.alert("请输入" + txt + "金额！", {icon: 0}, function (index) {
            layer.close(index);
            input.focus();
        });
        return false;
    }
    if (value < parseFloat(input.attr("data-min"))) {
        layer.alert("最低" + txt + "金额" + input.attr("data-min") + "元！", {icon: 0}, function (index) {
            layer.close(index);
            input.focus();
        });
        return false;
    }
    return true;
}
